package com.datastructure.test.maxsquare;

public class MaxSquare {

    public static void main(String[] args) {

    }

    public int solve (char[][] matrix) {
        if(matrix==null){
            return 0;
        }
        if(matrix.length==0){
            return 0;
        }
        //二维矩阵的宽和高
        int height = matrix.length;
        int width = matrix[0].length;
        int[][] dp = new int[height + 1][width + 1];
        int maxSide = 0;//最大正方形的宽
        for (int i = 1; i <= height; i++) {
            for (int j = 1; j <= width; j++) {
                if (matrix[i - 1][j - 1] == '1') {
                    //递推公式
                    dp[i][j] = Math.min(dp[i - 1][j], Math.min(dp[i - 1][j - 1], dp[i][j - 1])) + 1;
                    //记录最大的边长
                    maxSide = Math.max(maxSide, dp[i][j]);
                }
            }
        }
        //返回正方形的面积
        return maxSide * maxSide;
    }
}
